package com.mjf.ys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.BooleanUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mjf.ys.domain.FsYsProductClass;
import com.mjf.ys.domain.ProductEntity;
import com.mjf.ys.enums.YSApiEnum;
import com.mjf.ys.mapper.FsYsProductClassMapper;
import com.mjf.ys.service.FsYsProductClassService;
import com.mjf.ys.util.YSSendUtil;
import com.mjf.ys.vo.ProductClassVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * desc
 * </p>
 *
 * @author chenshoumeng
 * @date 2025/6/29 22:29
 */
@Slf4j
@Service
public class FsYsProductClassServiceImpl extends ServiceImpl<FsYsProductClassMapper, FsYsProductClass> implements FsYsProductClassService {

    @Autowired
    private YSSendUtil ysSendUtil;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void asyncProductClass() {
        List<ProductClassVO.PClassVO> datas = new ArrayList<>();
        String sendUrl = ysSendUtil.getSendUrl(YSApiEnum.YS_API_MATERIAL_CLASS_TREE_URL);
        Map<String, Object> resMap = new HashMap<>();
        log.info("商品同步，开始 {} ", sendUrl);
        resMap.put("pageSize", 500);
        resMap.put("totalCount", 10000);
        int i = 1;
        boolean  flag = true;
        List<FsYsProductClass> all = new ArrayList<>();
        while (flag) {
            log.info(" 第 {} 页 ", i);
            resMap.put("pageIndex", i);
            try {
                String repsp = ysSendUtil.reqForResp(sendUrl, resMap);
                System.out.println(repsp);
                // 解析接口
                ProductClassVO classVO = JSON.parseObject(repsp, ProductClassVO.class);
                if (classVO.getCode().equals("200")) {
                    datas.addAll(classVO.getData());
                } else {
                    log.error("获取商品分类失败，接口返回结果：{}", JSON.toJSONString(classVO));
                    flag = false;
                }
                i ++;
            } catch (Exception e) {
                log.error("商品同步异常", e);
                flag = false;
            }
        }
        if (CollectionUtils.isNotEmpty(datas)) {
            for (ProductClassVO.PClassVO vo : datas) {
                FsYsProductClass productClass = new FsYsProductClass();
                productClass.setId(vo.getId());
                productClass.setCode(vo.getCode());
                productClass.setName(vo.getName().getSimplifiedName());
                productClass.setParent(vo.getParent());
                productClass.setParentCode(vo.getParentCode());
                productClass.setProductClass(vo.getProductClass());
                productClass.setStopStatus(Boolean.TRUE.equals(vo.getStopStatus()) ? "true":"false");
                productClass.setIsOrder(vo.getOrder());
                productClass.setLevel(vo.getLevel());
                all.add(productClass);
            }
            saveOrUpdateBatch(all);
        }
        log.info("商品同步结束,处理数据：{}", datas.size());
    }


}
